<?php
/**
 * 车辆故障类别管理
 * @author hezhong
 */
class Break_down_category extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('car_fault_type_model');
    }

    public function index() {
        $param['typename'] = $this->input->get_post('typename');
        $param['start'] =  intval($this->input->get_post('per_page'));
        $param['length'] = 10;

        $data = $this->car_fault_type_model->select_list($param);


        $this->load->library('pagination');

        $config['base_url'] = site_url('c=break_down_category&m=index&typename='.$param['typename']);
        $config['total_rows'] = $data['count'];
        $config['per_page'] = 10;
        $config['cur_tag_open'] = '<span class="page_now">';
        $config['cur_tag_close'] = '</span>';

        $this->pagination->initialize($config);
        $data['pagelink'] = $this->pagination->create_links();

        
        $this->show_view("breakdownCategory", $data);
       
    }

    public function add() {
        $this->form_validation->set_rules('typename', '类别名称', 'required');

        if($this->form_validation->run() === FALSE) {
            $data['types'] = $this->car_fault_type_model->select_by_parentid();
            
            $this->show_view("breakdownCategoryAdd", $data);
           
        }else {
            $this->car_fault_type_model->insert_type();
            $this->show_message("添加车辆故障类别成功",site_url("c=break_down_category&m=index"));
        }
    }

    public function edit() {
        $typeid = $this->input->get_post('typeid');

        $this->form_validation->set_rules('typename', '类别名称', 'required');

        if($this->form_validation->run() === FALSE) {
            $data['types'] = $this->car_fault_type_model->select_by_parentid();

            $data['info'] =  $this->car_fault_type_model->select_by_id($typeid);

            $this->show_view("breakdownCategoryEdit", $data);
           
        }else {
            $parentid = $this->input->get_post('parentid');
            $typename = $this->input->get_post('typename');

            $this->car_fault_type_model->update($typeid, $typename, $parentid);
            $this->show_message("编辑车辆故障类别成功",site_url("c=break_down_category&m=index"));
        }
    }

    public function delete() {
        $typeid = $this->input->get_post('typeid');

        $this->car_fault_type_model->delete($typeid);
        $this->show_message("删除车辆故障类别成功",site_url("c=break_down_category&m=index"));
    }
}